package com.jeesite.modules.stock.web;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jeesite.modules.utils.CommonUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Objects;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.dp.entity.column.DpColumn;
import com.jeesite.modules.dp.service.column.DpColumnService;
import com.jeesite.modules.stock.entity.UserColumnsEntity;
import com.jeesite.modules.stock.service.UserColumnsService;

@RestController
@RequestMapping("${adminPath}/selfstock")
public class SelfStockDataControler {

    @Autowired
    private DpColumnService dpColumnService;

    @Autowired
    private UserColumnsService userColumnsService;

    @RequestMapping(value = "getTableColumns")
    @ResponseBody
    public Map<String, Object> getTableColumns(String userId) {
        List<DpColumn> columns = dpColumnService.findAllColList();
        userColumnsService.initUserColumns(userId, columns);
        List<UserColumnsEntity> userColumns = userColumnsService.getUserColumns(userId);
        Map<String, Object> rsmap = new HashMap<String, Object>();
        rsmap.put("showlist", getMaps(userColumns, true));
        rsmap.put("unshowlist", getMaps(userColumns, false));
        return rsmap;
    }

    private static @NotNull List<Map<String, Object>> getMaps(List<UserColumnsEntity> userColumns, boolean checked) {
        List<Map<String, Object>> voList = new ArrayList<>();
        for (UserColumnsEntity column : userColumns) {
            boolean ck = CommonUtils.isTrue(String.valueOf(column.getChecked()));
            if ((ck && !checked) || (checked && !ck)) {
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("key", column.getColumnKey());
            map.put("title", column.getColumnName());
            map.put("sorter", column.getIsSort());
            map.put("ellipsis", true);
            map.put("dataIndex", column.getColumnDataindex());
            map.put("type", column.getColumnType());
            map.put("checked", column.getChecked());
            map.put("sn", column.getSn());
            map.put("defaultSn", column.getDefaultSn());
            voList.add(map);
        }
        return voList;
    }
}
